package com.power.algorithm.structure.queue;

import java.util.Arrays;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/5/24 10:32 上午
 */
public class PriorityQueue {

    private Object[] array;
    private int CAPACITY = 10;
    private int size = 0;
    private final float EXPENSION_FACTORY = 1.5f;
    private int INDEX = 0;

    public PriorityQueue() {
        array = new Object[CAPACITY];
    }

    public void resize() {
        int newCapacity = Math.round(CAPACITY * EXPENSION_FACTORY);
        CAPACITY = newCapacity;
        array = Arrays.copyOf(array, newCapacity);
    }

    //数组中添加一个元素
    public void add(Object element) {
        if (isFull()) {
            resize();
        }
        array[INDEX++] = element;
        size++;
    }

    public void remove(Object element) {

        for (int i = 0; i < size; i++) {
            if (array[i] == element) {
                //删除元素   然后将后面的元素移动
                System.out.println(i);
                System.arraycopy(array, i + 1, array, i, size - i - 1);
                size--;
            }
        }
    }

    //获取数组的大小
    public int getSize() {
        return size;
    }

    //获取数组的容量
    public int getCapacity() {
        return CAPACITY;
    }

    //数组是否为空
    public boolean isEmpty() {
        if (size == 0) {
            return true;
        }
        return false;
    }

    //判断数组是否满
    public boolean isFull() {
        if (size == CAPACITY) {
            return true;
        }
        return false;
    }


    public void setIndex(int index, Object element) {
        if (index >= size || index < 0) {
            throw new IndexOutOfBoundsException("out of bound index");
        }
        array[index] = element;
    }

    public Object getIndex(int index) {
        if (index >= size || index < 0) {
            throw new IndexOutOfBoundsException("out of bound index");
        }
        return array[index];
    }

    public void display() {
        System.out.println("元素为::");
        for (int i = 0; i < size; i++) {
            System.out.print(array[i] + "  ");
        }
    }


    public static void main(String[] args) {
        PriorityQueue priorityQueue = new PriorityQueue();

        for (int i = 0; i < 12; i++) {
            priorityQueue.add(i);
        }
        priorityQueue.remove(5);
        priorityQueue.display();
    }
}
